﻿#include "AutoTextModule.h"

inline void sendChatMessage(const std::u8string_view text) {
	const auto localplayer = GameData::getLocalPlayer();
	C_TextPacket_view textPacket{localplayer->getNameTag()->getText(), fast_io::u8concat(localplayer->getUserId())};
	textPacket.message.setText(text);
	GameData::getClientInstance()->loopbackPacketSender->sendToServer(&textPacket);
}

void onKeyUpdate_AutoTextModule(uint8_t key, bool p) {
	if (p && key == AutoTextModule.getKeybind())
		AutoTextModule.toggle();

	if (AutoTextModule.isEnabled() && GameData::canUseMoveKeys()) {
		if (auto i = SettingManager::autotextbindings.find(static_cast<char8_t>(key)); i != SettingManager::autotextbindings.end()) {
			if (p)
				sendChatMessage(i->second);
		}
	}
}

IModule AutoTextModule{
	Category::MISC,
	{u8"AutoText", u8"自动文本", u8"自動文字", u8"自動テキスト"},
	{u8"Set small macros to send message quickly", u8"设置小型宏以快速发送消息", u8"設定小型宏以快速發送消息", u8"メッセージを迅速に送信するためのミニマクロの設定"},
	0,  // keybind
	fast_io::vector<SettingEntry>{},
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	&onKeyUpdate_AutoTextModule};
